/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import dao.SanphamDAO;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.Sanpham;

/**
 *
 * @author BlueZky
 */
public class gioHangController extends AbstractController {

    public gioHangController() {
    }

    @Override
    protected ModelAndView handleRequestInternal (
            HttpServletRequest request,
            HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        ModelAndView mv = null;
        HttpSession session = request.getSession();
        List<Sanpham> gioHang = (ArrayList<Sanpham>) session.getAttribute("gioHang");
        String action = request.getParameter("a");
        String PID = request.getParameter("pid");

        try {
            if (PID != null && PID.matches("^[0-9]*$")) {

                int pid = Integer.parseInt(PID);
                if (action.compareToIgnoreCase("a") == 0) {

                    if (gioHang == null) {
                        gioHang = new ArrayList<Sanpham>();
                    }

                    Sanpham sanPham = SanphamDAO.layThongTin(pid);
                    if (sanPham != null) {

                        Boolean same = false;
                        String soLuong = request.getParameter("soLuong");
                        int isoLuong = 0;
                        if (soLuong != null && soLuong.matches("^[0-9]*$")) {
                            isoLuong = Integer.parseInt(soLuong);
                        }
                        if (isoLuong > 0) {
                            // Neu da co trong gio hang  thi tang so luong len
                            for (int i = gioHang.size() - 1; i >= 0; i--) {
                                if (gioHang.get(i).getMasp() == pid) {
                                    gioHang.get(i).setSoluongton(gioHang.get(i).getSoluongton() + isoLuong);
                                    same = true;
                                    break;
                                }
                            }
                            // Neu chua co thi them moi san pham
                            if (!same) {
                                sanPham.setSoluongton(isoLuong);
                                gioHang.add(sanPham);
                            }
                        }
                        session.setAttribute("gioHang", gioHang);
                        response.sendRedirect(request.getRequestURI().substring(1));
                        return null;
                    }
                } else if (action.compareTo("d") == 0) {
                    if (gioHang != null) {
                        for (int i = gioHang.size() - 1; i >= 0; i--) {
                            if (gioHang.get(i).getMasp() == pid) {
                                gioHang.remove(i);
                                break;
                            }
                        }
                    }
                }
            } else {
                if (action != null && action.compareTo("u") == 0) {

                    if (gioHang != null) {
                        String[] soLuong = request.getParameterValues("soLuong");

                        for (int i = 0; i < gioHang.size(); i++) {
                            if (soLuong[i].matches("^[0-9]*$")) {
                                int isoLuong = Integer.parseInt(soLuong[i]);

                                if (isoLuong > 0) {
                                    gioHang.get(i).setSoluongton(isoLuong);
                                }
                            }
                        }
                    }
                }
            }

            //gioHang = SanphamDAO.layDanhSach(3);
            mv = new ModelAndView("User/index");
            mv.addObject("tag", "giohang");
            session.setAttribute("gioHang", gioHang);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            mv = null;
        }
        return mv;
    }
}
